<?php

class MallgoodsController extends Controller
{
    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,"type_id","type_id");
            $searchsql = Util::getSerarchStr($searchsql,"material_id","material_id");
            $searchsql = Util::getSerarchStr($searchsql,"shop_id","shop_id");
            $searchsql = Util::getSerarchStr($searchsql,"brands_id","brands_id");


            //获取count
            $totalcount = MallGoods::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = MallGoods::model()->findAll($searchsql);


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }



    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $name_short = Util::getParamNotEmpty("name_short");
            $type_id = Util::getParamNotEmpty("type_id");
            $type_name = Util::getParamNotEmpty("type_name");
            $material_id = Util::getParamNotEmpty("material_id");
            $material_name = Util::getParamNotEmpty("material_name");
            $shop_id = Util::getParamNotEmpty("shop_id");
            $shop_name = Util::getParamNotEmpty("shop_name");
            $brands_id = Util::getParamNotEmpty("brands_id");
            $brands_name = Util::getParamNotEmpty("brands_name");
            $pic_detail = Util::getParamNotEmpty("pic_detail");
            $price = Util::getParamNotEmpty("price");


            $detail_array = array();
            $detail_array["type_name"] = $type_name;
            $detail_array["material_name"] = $material_name;
            $detail_array["shop_name"] = $shop_name;
            $detail_array["brands_name"] = $brands_name;

            //添加
            $obj = new MallGoods();
            Util::initObjData($obj);


            $obj->name = trim($name);
            $obj->name_short = trim($name_short);
            $obj->type_id = $type_id;
            $obj->material_id = $material_id;
            $obj->shop_id = $shop_id;
            $obj->shop_name = $shop_name;
            $obj->brands_id = $brands_id;
            $obj->detail_info = json_encode($detail_array);
            $obj->pic_detail = $pic_detail;
            $obj->price = $price;



            Util::initObjData($obj);
            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $name_short = Util::getParamNotEmpty("name_short");
            $type_id = Util::getParamNotEmpty("type_id");
            $type_name = Util::getParamNotEmpty("type_name");
            $material_id = Util::getParamNotEmpty("material_id");
            $material_name = Util::getParamNotEmpty("material_name");
            $shop_id = Util::getParamNotEmpty("shop_id");
            $shop_name = Util::getParamNotEmpty("shop_name");
            $brands_id = Util::getParamNotEmpty("brands_id");
            $brands_name = Util::getParamNotEmpty("brands_name");
            $pic_detail = Util::getParamNotEmpty("pic_detail");
            $price = Util::getParamNotEmpty("price");

            //验证存在
            $obj = MallGoods::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            //修改信息

            $obj->name = trim($name);
            $obj->name_short = trim($name_short);
            $obj->type_id = $type_id;
            $obj->material_id = $material_id;
            $obj->shop_id = $shop_id;
            $obj->brands_id = $brands_id;
            $obj->pic_detail = $pic_detail;
            $obj->price = $price;

            $detail_array =  json_decode($obj->detail_info,true);
            $detail_array["type_name"] = $type_name;
            $detail_array["material_name"] = $material_name;
            $detail_array["shop_name"] = $shop_name;
            $detail_array["brands_name"] = $brands_name;
            $obj->detail_info = json_encode($detail_array);


            Util::initObjData($obj);
            //保存
            Util::saveObjData($obj);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallGoods::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            Util::deleteObjData($obj);

            Util::saveObjData($obj);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取商品对应的规格属性列表
    *
    */
    public function actionGetSpecsList()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $goods_id = Util::getParamNotEmpty("goods_id");



            //验证商品是否存在
            $goodobj = MallGoods::model()->findByPK($goods_id);
            Util::checkExistAndNotDelete($goodobj);

            $material_id = $goodobj->material_id;

            //验证二级菜单是否存在
            $materialobj = MallMaterial::model()->findByPK($material_id);
            Util::checkExistAndNotDelete($materialobj);


            $searchsql = Util::initSearchSql();

            //根据material_id查找到对应的模板列表
            $templatelist = MallGoodsService::findtemplatebymaterial($searchsql,$material_id);

            //根据template列表查找到对应的规格列表
            $specslist = MallGoodsService::findspecsbytemplate($searchsql,$templatelist);

            //根据specs列表查找到对应的规格列表
            $returnlist = MallGoodsService::findspecsandattrs($goods_id,$specslist);



                Util::responseList(count($returnlist), $returnlist);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 设置商品对应的规格属性
    *
    */
    public function actionsetSpecsList()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $goods_id = Util::getParamNotEmpty("goods_id");
            $specs_id = Util::getParamNotEmpty("specs_id");

            $attrs_id = Util::getParam("attrs_id",null);
            $value = Util::getParam("value",null);


            //验证goods是否存在
            $goodsobj = MallGoods::model()->findByPK($goods_id);
            Util::checkExistAndNotDelete($goodsobj);

            $searchsql = Util::initSearchSql();

            //根据specs_id查找到对应的规格
            $specs = MallSpecs::model()->findbyPK($specs_id);

            $attrs_name = '';
            if($specs->type == 0){
                //根据attrs_id查找到对应的属性
                $attrs = MallAttrs::model()->findbyPK($attrs_id);
                $attrs_name = $attrs -> name;
            }

            //验证是否存在于关系表
            $mall_goods_specs  = MallGoodsService::checkgoodsandspecs($goods_id,$specs_id);

            if(empty($mall_goods_specs) == true){
                //新增
                $obj = new MallGoodsSpecs();

                $obj->goods_id = $goods_id;
                $obj->specs_id = $specs_id;
                $obj->specs_name = $specs->name;
                $obj->specs_type = $specs->type;
                $obj->unit_id = $specs->unit_id;
                $obj->unit_name = $specs->unit_name;
                $obj->attrs_id = $attrs_id;
                $obj->attrs_name =$attrs_name;
                if($specs->type == 0){
                    $obj->value = $attrs_id;
                }else{
                    $obj->value = $value;
                }


                Util::saveObjData($obj);

            }else{
                //修改

                $mall_goods_specs->specs_name = $specs->name;
                $mall_goods_specs->specs_type = $specs->type;
                $mall_goods_specs->unit_id = $specs->unit_id;
                $mall_goods_specs->attrs_id = $attrs_id;
                $mall_goods_specs->attrs_name = $attrs_name;
                $mall_goods_specs->unit_name = $specs->unit_name;
                if($specs->type == 0){
                    $mall_goods_specs->value = $attrs_id;
                }else{
                    $mall_goods_specs->value = $value;
                }

                if($mall_goods_specs->is_delete = 1){
                    Util::recoverObjData($mall_goods_specs);
                }


                Util::saveObjData($mall_goods_specs);

            }

            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取单个商品详情
    *
    */
    public function actionGetGoodObj()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $id = Util::getParamNotEmpty("id");


            $obj = MallGoods::model()->findByPK($id);

            Util::checkExistAndNotDelete($obj);


            Util::responseObj($obj);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 删除
    */
    public function actionDeleteSpecsList()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $goods_id = Util::getParamNotEmpty("goods_id");
            $specs_id = Util::getParamNotEmpty("specs_id");

            //删除
            $obj  = MallGoodsSpecs::model()->find("goods_id=:goods_id and  specs_id=:specs_id",
                array(":goods_id" => $goods_id,":specs_id" => $specs_id));

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);


                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


}